﻿using Microsoft.AspNetCore.Mvc;
using Zhaoxi.NET6.Interfaces;
using Zhaoxi.NET6.Models;

namespace NET6Demo.WebApi.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class ReturnResultController : ControllerBase
    {
        private readonly ILogger<ReturnResultController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger"></param>
        public ReturnResultController(ILogger<ReturnResultController> logger)
        {
            _logger = logger;
        }

        #region 返回Specific-type

        /// <summary>
        /// 返回ApiResult
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetStudentApiResult")]
        public ApiResult<Student> GetStudentApiResult(int id) => new ApiResult<Student>()
        {
            Success = true,
            Message = "查询成功",
            Data = new Student()
            {
                Id = 23,
                Name = "Richard",
                Age = 36
            }
        };

        /// <summary>
        /// Specific-type==返回User
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetStudentById/{id:int}")]
        public Student GetStudentById(int id) => new Student()
        {
            Id = 1,
            Age = 36,
            Name = "张三"
        };

        /// <summary>
        /// Specific-type==返回集合
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetStudentList")]
        public IEnumerable<Student> GetStudentList() => new List<Student>()
        {
            new Student()
            {
                Id = 1,
                Age = 36,
                Name = "张三"
            }, new Student()
            {
                Id = 1,
                Age = 36,
                Name = "李四"
            }
        };


        /// <summary>
        /// Specific-type==异步返回   在asp.net core 3.1之后
        /// </summary>
        /// <param name="studentService"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAsyncUserList")]
        public async IAsyncEnumerable<Student> GetAsyncUserList([FromServices] IStudentService studentService)
        {
            var studentlist = studentService.GetUserListAsync();
            // 使用GetAuthorsAsync异步方法，不用authors查询完毕，就会进入下一步迭代返回authors
            await foreach (var student in studentlist)
            {
                yield return student;
            }
        }

        /// <summary>
        /// 返回一个int类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetInt")]
        public int GetInt()
        {
            return 12345;
        }

        /// <summary>
        /// 返回一个String类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetString")]
        public String GetString()
        {
            return "this is String";
        }

        #endregion

        #region 返回IActionResult类型
        /// <summary>
        /// 返回IActionResult
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetStudentApiResultIActionResult")]
        public IActionResult GetStudentApiResultIActionResult(int id) => new JsonResult(new ApiResult<Student>()
        {
            Success = true,
            Message = "查询成功",
            Data = new Student()
            {
                Id = 23,
                Name = "Richard",
                Age = 36
            }
        });


        /// <summary>
        /// 返回IActionResult==GetStudentJson
        /// </summary>
        /// <returns></returns>
        [Route("GetStudentJson")]
        [HttpGet]
        public IActionResult GetStudentJson()
        {
            //返回Json对象
            return new JsonResult(new Student()
            {
                Id = 1,
                Age = 36,
                Name = "张三"
            });

            ////响应Nofound
            //return NotFound("No records");

            ////响应Ok
            ////return Ok(); // 必须有Ok等方法包装
        }

        #endregion

        #region 返回ActionResult<T>

        /// <summary>
        /// 返回ActionResult 泛型==返回 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetStudentNew")]
        public ActionResult<Student> GetStudentNew()
        {
            //返回Json对象
            return new Student()
            {
                Id = 1,
                Age = 36,
                Name = "张三"
            };
        }


        /// <summary>
        /// 返回泛型的ActionResult
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetStudentApiResultActionResultGeneric")]
        public ActionResult<ApiResult<Student>> GetStudentApiResultActionResultGeneric(int id) => new ApiResult<Student>()
        {
            Success = true,
            Message = "查询成功",
            Data = new Student()
            {
                Id = 23,
                Name = "Richard",
                Age = 36
            }
        };


        #endregion



    }
}